package com.farm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.entity.Product;
import com.farm.entity.User;
import com.farm.mapper.ProductMapper;
import com.farm.mapper.UserMapper;
import com.farm.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<Product> getProducts(Integer page, Integer size, String category, String search) {
        Page<Product> pageParam = new Page<>(page, size);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        
        // 只显示已上架的商品
        queryWrapper.eq("status", 1);
        
        // 按分类筛选
        if (category != null && !category.isEmpty()) {
            queryWrapper.eq("category", category);
        }
        
        // 按名称或描述搜索
        if (search != null && !search.isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                .like("name", search)
                .or()
                .like("description", search)
            );
        }
        
        // 按创建时间倒序排序
        queryWrapper.orderByDesc("create_time");
        
        Page<Product> productPage = productMapper.selectPage(pageParam, queryWrapper);
        
        // 填充农户信息
        for (Product product : productPage.getRecords()) {
            User farmer = userMapper.selectById(product.getFarmerId());
            product.setFarmer(farmer);
        }
        
        return productPage;
    }

    @Override
    public Product getProductById(Long id) {
        Product product = productMapper.selectById(id);
        if (product != null) {
            User farmer = userMapper.selectById(product.getFarmerId());
            product.setFarmer(farmer);
        }
        return product;
    }

    @Override
    public List<String> getAllCategories() {
        return productMapper.selectAllCategories();
    }

    @Override
    @Transactional
    public void addProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    @Transactional
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    @Transactional
    public void deleteProduct(Long id) {
        productMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void auditProduct(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        productMapper.updateById(product);
    }

    @Override
    public List<Product> getProductsByFarmerId(Long farmerId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farmer_id", farmerId);
        return productMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public boolean updateStock(Long productId, Integer quantity) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", productId)
                    .ge("stock", -quantity)  // 确保库存足够
                    .setSql("stock = stock + " + quantity);
        return productMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public Page<Product> getProductList(Integer page, Integer size, String category) {
        Page<Product> pageParam = new Page<>(page, size);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        
        // 按分类筛选
        if (category != null && !category.isEmpty()) {
            queryWrapper.eq("category", category);
        }
        
        // 按创建时间倒序排序
        queryWrapper.orderByDesc("create_time");
        
        Page<Product> productPage = productMapper.selectPage(pageParam, queryWrapper);
        
        // 填充农户信息
        for (Product product : productPage.getRecords()) {
            User farmer = userMapper.selectById(product.getFarmerId());
            product.setFarmer(farmer);
        }
        
        return productPage;
    }
} 